package graphicView;

import java.io.IOException;
import java.util.Vector;

import javax.swing.*;
import java.awt.*;

import model.ClassImpl;
import model.DiagramImpl;
import model.ClassEditor;

/**
 * GraphicController is the startup class and controller for the java swing
 * version of word search. It owns the graphical user interface and puzzle.
 * 
 * @author David Hutchens - Modified by Team EBE
 * 
 * GraphicController.java is modified by Team EBE to handle the ClassGUI.java 
 * and RelationshipGUI.java
 */
public class GraphicController {
	private static GraphicController sharedApp;
	// private GraphicUI ui;
	private DiagramUserInterface ui;
	private JFrame theFrame;
	private RelationshipGUI rg;
	private JFrame relFrame;
	private ClassGUI cg;
	private JFrame classFrame;
	private DiagramImpl theDiagram;
	private ClassEditor theClassEditor;
	private ClassGraphicObject cgo;
	private Vector<ClassGraphicObject> classesToDisplay;
	private ActiveClassNotifier activeClassNotifier;
	private Vector<RelationshipGraphicObject> relationshipsToDisplay;

	/**
	 * Constructs the object, creating the model and view (with the frame and
	 * user interface)
	 */
	private GraphicController() {
		// Set the look and feel (for Macs too).
		if (System.getProperty("mrj.version") != null) {
			System.setProperty("apple.laf.useScreenMenuBar", "true");
		}
		JFrame.setDefaultLookAndFeelDecorated(true);

		theFrame = new JFrame("UML");
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		theDiagram = new DiagramImpl();
		theDiagram = buildTestDiagram(theDiagram);
		
		theClassEditor = new ClassEditor(theDiagram.getClassImpl(0) );
		
		classFrame = new JFrame("Edit ClassImpl");
		classFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		cg = new ClassGUI(this, classFrame, theClassEditor);
		
		activeClassNotifier = new ActiveClassNotifier();
		activeClassNotifier.addObserver(theClassEditor);
		activeClassNotifier.addObserver(cg);
		classesToDisplay = new Vector<ClassGraphicObject>();
		classesToDisplay.add(new ClassGraphicObject(diagram().getClassImpl(0)));
		classesToDisplay.add(new ClassGraphicObject(diagram().getClassImpl(1)));
		relationshipsToDisplay = new Vector<RelationshipGraphicObject>();
		relationshipsToDisplay.add(new RelationshipGraphicObject(diagram().getRelationshipImpl(0)));
		// ui = new GraphicUI(this, theFrame);
		ui = new DiagramUserInterface(this, theFrame, classesToDisplay, relationshipsToDisplay, activeClassNotifier);
		theFrame.pack();
		theFrame.setVisible(true);

		/*
		 * relFrame = new JFrame("Edit RelationshipImpl");
		 * relFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); rg = new
		 * RelationshipGUI(this, theFrame); relFrame.pack();
		 * relFrame.setVisible(true);
		 */
	}
	
	private DiagramImpl buildTestDiagram(DiagramImpl theDiagram){
		theDiagram.addClass("Test ClassImpl 1");
		theDiagram.addClass("Test ClassImpl 2");
		theDiagram.getClassImpl(0).addAttribute("attribute 1", "string", '+');
		theDiagram.getClassImpl(0).addAttribute("attribute 2", "char", '-');
		theDiagram.getClassImpl(0).addOperation("operation 1", "void", '#');
		theDiagram.getClassImpl(1).addAttribute("attribute 1", "string", '+');
		theDiagram.getClassImpl(1).addAttribute("attribute 2", "char", '-');
		theDiagram.getClassImpl(1).addOperation("operation 1", "void", '#');
		theDiagram.getClassImpl(1).setoX(300);
		theDiagram.addRelationship(theDiagram.getClassImpl(0).anchorAt(3), theDiagram.getClassImpl(1).anchorAt(7));
		//theDiagram.getClassImpl(0).getOperations().get(0).add_parameter("name", "string");
		return theDiagram;
	}
	
	public void addClass(int oX, int oY){
		ClassImpl toAdd = new ClassImpl(oX, oY);
		theDiagram.addClass(toAdd);
		classesToDisplay.add(new ClassGraphicObject(toAdd));
	}

	/**
	 * Returns the puzzle
	 * 
	 * @return the puzzle
	 */
	public DiagramImpl diagram() {
		return theDiagram;
	}

	/**
	 * Returns the graphical user interface
	 * 
	 * @return the graphcial user interface
	 */
	/*
	 * public GraphicUI graphicalInterface ( ) { return ui; }
	 */

	public DiagramUserInterface graphicalInterface() {
		return ui;
	}

	/**
	 * creates (if necessary) and returns the singleton instance
	 * 
	 * @return the singleton shared instance
	 */
	public static GraphicController sharedInstance() {
		if (sharedApp == null) {
			sharedApp = new GraphicController();
		}
		return sharedApp;
	}

	public void openRelation() {

		if (System.getProperty("mrj.version") != null) {
			System.setProperty("apple.laf.useScreenMenuBar", "true");
		}

		JFrame.setDefaultLookAndFeelDecorated(true);

		relFrame = new JFrame("Edit RelationshipImpl");
		//relFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		rg = new RelationshipGUI(this, relFrame);

		relFrame.pack();
		relFrame.setVisible(true);

		// RelationshipGUI();

	}

	public RelationshipGUI graphicalInterface1() {
		return rg;
	}

	public void openClass() {

		
		
		

		classFrame.pack();
		classFrame.setVisible(true);

		// ClassGUI();

	}

	public ClassGUI graphicalInterface2() {
		return cg;
	}
	
	/**
	 * Creates the runnable job that runs the graphical interface
	 * 
	 * @param args
	 *            are not used
	 */
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				sharedInstance();
			}
		});
	}
}
